package com.gxa.controller.commlife;

import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.commlife.CouponUseDto;
import com.gxa.service.commlife.CouponUseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Api(tags = "消费券使用")
@RestController
@RequestMapping("/coupon-use")
public class CouponUseController {
    @Autowired
    private CouponUseService couponUseService;

//    List<CouponUse> couponUseList;
//    Date date = new Date("2022/10/01");
//    Date date1 = new Date("2022/10/02");

//    public CouponUseController(){this.init();}
//    void init(){
//        couponUseList= new ArrayList<>();
//        User user = new User();
//        CouponManagement couponManagement = new CouponManagement(1,"餐饮优惠券",30,"24小时内",date,date1,100,30,0,0);
//        CouponUse couponUse = new CouponUse(1,couponManagement,date,date1,"DD21309123123",user,0);
//        couponUseList.add(couponUse);
//        CouponManagement couponManagement1 = new CouponManagement(2,"餐饮优惠券",30,"24小时内",date,date1,100,30,0,0);
//        CouponUse couponUse1 = new CouponUse(2,couponManagement,null,null,"DD21309123123",user,0);
//        couponUseList.add(couponUse1);
//    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询消费券使用情况")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        try {
            Map<String, Object> map = this.couponUseService.queryList(pageNum, pageSize);
            Result<Map<String, Object>> result = Result.success(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @PostMapping("search")
    @ApiOperation(value = "搜索消费券使用情况")
    public Result search(@RequestBody CouponUseDto couponUseDto, Integer pageNum,Integer pageSize){
        try {
            Map<String, Object> map = this.couponUseService.queryListBySearch(couponUseDto, pageNum, pageSize);
            Result<Map<String, Object>> result = Result.success(map);
            result.setMsg("搜索成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("搜索失败");
        }
        }

    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("export")
    @ApiOperation(value = "导出消费劵使用记录")
    public R export(){
        return R.ok("success");
    }

}
